<?php
namespace app\admin\logic;
use app\common\controller\TyLogic;

class BaseLogic{

    use TyLogic;

    /**
     * 写入后台操作
     * @return int|\think\response\Json
     */
    public function write_ao(){
        //检查后台用户规则
        $r = $this->check_admin_rule();
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['detail'];
        
        $ao_data = [];
        $ao_data['adminid'] = $a_detail['id'];
        $ao_data['username'] = $a_detail['username'];
        $ao_data['ip'] = \think\facade\Request::ip();
        $ao_data['request_url'] = \think\facade\Request::url(true);
        $ao_data['controller_action'] = \think\facade\Request::controller() . '-' . \think\facade\Request::action();
        $ao_data['method'] = \think\facade\Request::method();
        $ao_data['params'] = json_encode(\think\facade\Request::param(),JSON_UNESCAPED_UNICODE);
        $ao_data['result'] = '';
        $r = model('admin_operate')->insert($ao_data);
        return 1;
    }

    /**
     * 检查后台用户权限规则
     * @return mixed
     */
    public function check_admin_role_rule(){
        $maps = [];
        $maps[] = ['openid','=',session('admin_openid')];
        $maps[] = ['role','in',[1,2]];
        $detail = model('user')->get_detail($maps,'id,role');
        if(empty($detail)){
            return $this->get_response(0,'您没有权限进行此操作');
        }
        return $this->get_response(1,'OK');
    }
}